﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace SachVietDLL.BLL
{
    public class CategoryBLL
    {
        private BookDatabaseEntities db = new BookDatabaseEntities();
        public DbSet<Book_Category> GetAllCategory()
        {
            return db.Book_Category;
        }        

        public Book_Category GetCategoryByID(string id)
        {
            return db.Book_Category.Find(id);
          
        }

        public void Create(Book_Category book_category)
        {
            int cout = db.Book_Category.Count() + 1;
            string categoryID = "CTGRID" + cout.ToString("0000");
            book_category.book_Category_ID = categoryID;
            db.Book_Category.Add(book_category);
            db.SaveChanges();
        }

        public void Edit(Book_Category book_category)
        {
            db.Entry(book_category).State = EntityState.Modified;
            db.SaveChanges();            
        }

        public void Delete(string id)
        {
            BookBLL bookBll = new BookBLL();
            List<BookDetail> list = bookBll.GetAllBooksByCategory(id);
            foreach (var b in list)
                bookBll.Delete(b.book_ID);

            Book_Category book_category = db.Book_Category.Find(id);
            db.Book_Category.Remove(book_category);
            db.SaveChanges();
        }

    }
}